//
//  CoreGraphic.m
//  LGFDemo
//
//  Created by lee on 2018/8/13.
//  Copyright © 2018年 lee. All rights reserved.
//


#import "CoreGraphic.h"
#import "UIView+category.h"

typedef NS_ENUM(NSUInteger,LGFViewType)
{
    LGFViewTypeText,
    LGFViewTypeImage,
    LGFViewTypeCircle,
    LGFViewTypeRangtle,
    LGFViewTypeUnknow
};

@interface CoreGraphic()

@property(nonatomic,assign)NSUInteger diameter;
@property(assign ,nonatomic) CGPoint  point;
@property(assign ,nonatomic) LGFViewType  viewType;
@property(copy ,nonatomic) NSString  *text;
@property(copy ,nonatomic) NSDictionary  *attributes;
@property(assign ,nonatomic) CGPoint  lastOrigin;//图形画完的最后点
@property(copy ,nonatomic) NSString  *type;//图片类型
@end


@implementation CoreGraphic

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.

*/

- (void)drawRect:(CGRect)rect {
    // Drawing code
    if (CGRectEqualToRect(rect, CGRectZero))
    {
        return;
    }
    CGContextRef context=UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextFillRect(context, rect);
    CGContextDrawPath(context, kCGPathFill);
    if (_viewType==LGFViewTypeCircle)
    {
        [self drawShape];
    }
    else if (_viewType==LGFViewTypeText)
    {
        [self drawText];
    }
    else if (_viewType==LGFViewTypeImage)
    {
        [self drawImage];
    }
}


-(void)drawACircleWithDiameter:(NSUInteger)diameter Point:(CGPoint)point
{
    if (diameter==0)
    {
        return;
    }
    _viewType=LGFViewTypeCircle;
    _diameter=diameter;
    _point=point;
    [self setNeedsDisplay];
}

-(void)drawText:(NSString *)text attributes:(NSDictionary *)attibutes
{
    if (text.length==0)
    {
        return;
    }
    _text=text;
    _viewType=LGFViewTypeText;
    if (attibutes.count==0)
    {
        _attributes=@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:[UIColor blackColor]};
    }
    else
    {
        _attributes=attibutes;
    }
    [self setNeedsDisplay];
}

-(void)drawImage:(NSString *)image type:(NSString *)type
{
    if (image.length==0)
    {
        return;
    }
    _text=image;
    if (type==nil)
    {
        _type=@"png";
    }
    else
    {
        _type=type;
    }
    _viewType=LGFViewTypeImage;
    [self setNeedsDisplay];
}

////绘制文字
-(void)drawText
{
    // 文本段落样式
    NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle alloc] init];
    textStyle.lineBreakMode = NSLineBreakByWordWrapping; // 结尾部分的内容以……方式省略 ( "...wxyz" ,"abcd..." ,"ab...yz")
    textStyle.alignment = NSTextAlignmentLeft; //（两端对齐的）文本对齐方式：（左，中，右，两端对齐，自然）
    textStyle.lineSpacing = 5; // 字体的行间距
    textStyle.firstLineHeadIndent = 5.0; // 首行缩进
    textStyle.headIndent = 0.0; // 整体缩进(首行除外)
    textStyle.tailIndent = 0.0; //
    textStyle.minimumLineHeight = 20.0; // 最低行高
    textStyle.maximumLineHeight = 20.0; // 最大行高
    textStyle.paragraphSpacing = 15; // 段与段之间的间距
    textStyle.paragraphSpacingBefore = 22.0f; // 段首行空白空间/* Distance between the bottom of the previous paragraph (or the end of its paragraphSpacing, if any) and the top of this paragraph. */
    textStyle.baseWritingDirection = NSWritingDirectionLeftToRight; // 从左到右的书写方向（一共➡️三种）
    textStyle.lineHeightMultiple = 15; /* Natural line height is multiplied by this factor (if positive) before being constrained by minimum and maximum line height. */
    textStyle.hyphenationFactor = 1; //连字属性 在iOS，唯一支持的值分别为0和1
    // 文本属性
    NSMutableDictionary *textAttributes = [[NSMutableDictionary alloc] initWithCapacity:5];
    [textAttributes setValuesForKeysWithDictionary:_attributes];
    // NSParagraphStyleAttributeName 段落样式
    [textAttributes setValue:textStyle forKey:NSParagraphStyleAttributeName];
    
    CGSize size=[_text boundingRectWithSize:CGSizeMake(self.width-30, HUGE) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:_attributes context:nil].size;
    [_text drawInRect:CGRectMake(10, 30, self.width-30, ceilf(size.height)) withAttributes:textAttributes];
}
///绘制图片
-(void)drawImage
{
    NSString *imagePath=[[NSBundle mainBundle] pathForResource:_text ofType:_type];
    UIImage *image=[UIImage imageWithContentsOfFile:imagePath];
    CGRect rect=CGRectMake(_lastOrigin.x, _lastOrigin.y+10, image.size.width, image.size.height);
    if (image.size.width>self.width)
    {
        rect.size.height=image.size.height*self.width/image.size.width;
        rect.size.width=self.width;
    }
    if (rect.size.height>self.height)
    {
        rect.size.height=self.height;
        rect.size.width=rect.size.width*self.height/rect.size.height;
    }
    [image drawInRect:rect];
//    CGContextDrawImage(context, rect, image.CGImage);
//    CGContextDrawTiledImage(context, rect, image.CGImage);
    _lastOrigin=CGPointMake(_lastOrigin.x, CGRectGetMaxY(rect));
}
///绘制形状
-(void)drawShape
{
    CGContextRef context=UIGraphicsGetCurrentContext();
    //如果是圆形的话
    CGContextSetLineWidth(context, 1);
    CGContextSetStrokeColorWithColor(context, [UIColor purpleColor].CGColor);
    CGContextAddEllipseInRect(context, CGRectMake(_point.x, _point.y, _diameter, _diameter));
    CGContextDrawPath(context, kCGPathStroke);
    
}

@end
